package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Hotel;
import cn.wolfcode.trip.base.domain.HotelDetail;
import cn.wolfcode.trip.base.query.HotelDetailQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IHotelDetailService;
import cn.wolfcode.trip.base.service.IHotelService;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("/hotelDetail")
public class HotelDetailController {
    @Autowired
    private IHotelDetailService hotelDetailService;
    @Autowired
    private IHotelService hotelService;

    /**
     * 列表房型
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") HotelDetailQueryObject qo, Model model) {
        model.addAttribute("pageInfo",hotelDetailService.query(qo));
        List<Hotel> list = hotelService.list();
        model.addAttribute("hotels",list);
        return "hotelDetail/list";
    }

    /**
    admin更新和新增房型
     */
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(HotelDetail entity, MultipartFile img) {
        //先处理上传图片的问题
        if (img != null && img.getContentType().startsWith("image") && img.getSize() > 0) {
            //把图片上传到本地图库
            String url = UploadUtil.upload(img);
            //替换旧的图片地址
            entity.setImgUrl(url);
        }
        hotelDetailService.saveOrUpdate(entity);
        return "redirect:/hotelDetail/list.do";
    }

}
